import { reject } from "lodash";
import http from "../../../api/fastposter";

import storage from '@/utils/storage'

import { GRADE } from '@/utils/const.js'

function removeTrailingSlash(str) {
    if (str.endsWith('/')) {
        return str.slice(0, -1); // 截取从索引0到倒数第2个字符之间的内容
    }
    return str; // 如果最后一个字符不是斜杠，则原样返回字符串
}

const state = {
    debug: false,
    showLogin: false,
    showCode: false,
    showChangePwd: false,
    showTokenManager: false,
    showUserManager: false,
    showBatch: false,
    batchPosterId: 0,   // 批量海报ID
    batchId: 0,   // 批量ID
    user: {
        username: '',
        type: 1,
        auth:''
    },
    fonts: [],
    token: '',
    payList: '',
}

const getters = {
    debug() {
        return state.debug
    },
    isLogin() {

        return state.user.username !== ''
    },
    token() {
        return http.token()
    },
    apiUrl() {
        return removeTrailingSlash(http.baseURL)
    },
    uploadUrl() {
        return http.baseURL + 'api/upload'
    },
    fonts() {
        return state.fonts
    },
    // 用户的会员等级
    grade() {
        return GRADE.GRADELIST[state.user.auth]
    },
    // 用户的会员等级名称
    gradeName() {

        return GRADE.GRADENAMELIST[state.user.auth]
    }
}

const actions = {
    setDebug({ commit }, flag) {
        commit('setDebug', flag)
    },
    changepwd({ commit }, playload) {
        return new Promise((resolve) => {
            http.changepwd(playload, r => {
                if (r.code === 0) {
                    resolve(r)
                }
            })
        });
    },
    login({ commit }, playload) {
        return new Promise((resolve) => {
            http.login(playload, r => {
                if (r.code === 0) {
                    commit('login', r)
                    actions.getUserInfo({ commit })
                    resolve(r)
                }
            })
        });
    },
    logout({ commit }, playload) {
        return new Promise((resolve) => {
            http.logout(playload, r => {
                if (r.code === 0) {
                    commit('logout', r)
                    resolve(r)
                }
            })
        });
    },
    getUserInfo({ commit }, playload) {

        return new Promise((resolve, reject) => {
            http.userinfo(playload, r => {

                if (r.code === 0) {
                    commit('setUserInfo', r)
                    actions.loadFonts({ commit })
                    resolve(r)
                }
            }, err => {
                console.log(err, '错误')
            })
        });
    },
    loadFonts({ commit }, playload) {
        return new Promise((resolve) => {
            http.http.get('api/fonts').then(r => {
                if (r.code === 0) {
                    commit('setFonts', r)
                    // 异步加载字体
                    r.data.fonts.forEach(fn => http.loadfont(fn.code))
                    resolve(r)
                }
            })
        })
    },
    getPayList({ state, commit }) {
        return new Promise((resolve, reject) => {
            if (state.payList) {
                resolve(state.payList)
            } else {
                if (state.token) {
                    // http.reqUserPackageList().then(res => {
                    //     commit('setPayList', res)
                    //     resolve(res)
                    // })

                    http.reqUserPackageList({ package_id:'4' }).then(res => {
                        commit('setPayList', res)
                        resolve(res)
                      })
                        .finally(() => {
                          
                        })
                } else {
                    http.reqOvertPackageList({package_id:4}).then(res => {
                        commit('setPayList', res)
                        resolve(res)
                    }).finally(() => {

                        })
                }

            }
        })
    },
}
const mutations = {
    setDebug(state, flag) {
        state.debug = flag
    },
    login(state, r) {
        state.token = r.data.token
        localStorage.setItem(process.env.VUE_APP_FP_TOKEN, r.data.token)
        state.showLogin = false
    },
    logout(state, r) {
        localStorage.removeItem(process.env.VUE_APP_FP_TOKEN)
        state.token = ''
        let x = {
            username: '', type: 1
        }
        state.user = { ...x }
    },
    setUserInfo(state, r) {
        
        state.token = localStorage.getItem(process.env.VUE_APP_FP_TOKEN)
        // storage.set('userInfo', ...r.data.user)
        state.user = { ...r.data.user }
    },
    setFonts(state, r) {
        state.fonts = r.data.fonts
    },
    setPayList(state, val) {
    state.payList = val
  },
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations,
}
